<?php
namespace App\Models;

use App\Common\tbCommon;
Use DB;
use App\Models\TagM;
use Illuminate\Database\Eloquent\Model;

class ImageM extends Model
{
    public static  $STable="lee_icon_image";
    protected  $table="lee_icon_image";
    public $incrementing=false;
    public $primaryKey='uuid';
    
   
    /**
     * 添加多条数据记录
     * @param array $datas 数据记录，二维数组，
     * 如[
     *      ['iconid'=>iconid1,'id'=>id1,'path'=>imagepath1],
     *      ['iconid'=>iconid1,'id'=>id2,'path'=>imagepath2]
     *  ]
     */
     static public function addItems(array $datas){
        // 批量插入数据
        $tbName=ImageM::$STable;
        $newDatas=tbCommon::addMissColumnValue($tbName, $datas);
        DB::table($tbName)->insert($newDatas);
        
    }
    
    /**
     * 删除单个数据记录
     * @param uuid $id,待删除数据的主键
     */
    static public function deltItem($id){
        ImageM::where('id',$id)->delete($id);
    }
    
    /**
     * 批量删除
     * @param array $ids,待删除数据的 id集合
     * 如： array(id1,id2,id3)
     */
    static public function deltItems(array $ids){
        //ImageM::destroy($ids);
        ImageM::whereIn('id',$ids)->delete();
    }
    
    /**
     * 依据 icon的id值删除数据
     * @param uuid $iconId,icon的uuid值
     */
    static public function deltByIconId($iconId){
        ImageM::where('iconid','=',$iconId)->delete();
    }
    
    /**
     * 根据 icon的fid 返回 所有image的uuid
     * @param bigint $icnfid
     * @return image 的uuid集合
     * likes: [imguuid1,imguuid2,imguuid3]
     */
    static public function getImgIds($icnfid){
        $icnT=IconM::$STable;
        $imgT=self::$STable;
        $sql="select img.id as imgid from $imgT as img 
            where img.iconid =
            (select id from $icnT as icn where icn.fid =$icnfid)";
        $imgIds=DB::select($sql);
        return array_column($imgIds, 'imgid');
    }
    
    
}

?>